﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;

namespace UploadBlobToAzure
{       
    public partial class Form1 : Form
    {
        
        private void btnConnect_Click(object sender, EventArgs e)
        {
            field_validator fv = new field_validator();
            if (!fv.checkIfFileExists("uploadblobtoazure.xml"))
            {
                MessageBox.Show("There is no config file present in your current working directory,please reconfigure the application and try again");
                btnConnect.Enabled = false;
                btnEdit.Enabled = false;
                btnRemoveList.Enabled = false;
                cmbStorageSelect.Items.Clear();
                cmbStorageSelect.Enabled = false;
                manageBlobToolStripMenuItem.Enabled = false;
                chkSSL.Enabled = false;
                txtSearch.Enabled = false;
                btnDisconnect.Enabled = false;
                treeContainerList.Nodes.Clear();
                gridContainer.Rows.Clear();
                //this.Close();
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                if (fv.testIfFieldEmpty(cmbStorageSelect.Text) || (cmbStorageSelect.Text == "---Select A Storage Account To Connect To---"))
                {
                    MessageBox.Show("Please Select the Storage Account Name from the Drop Down");
                }
                else
                {
                    storageAccName = cmbStorageSelect.SelectedItem.ToString();
                    if (chkSSL.Checked == true)
                    {
                        blobEndPoint = "https://" + cmbStorageSelect.SelectedItem + ".blob.core.windows.net/";
                    }
                    else
                    {
                        blobEndPoint = "http://" + cmbStorageSelect.SelectedItem + ".blob.core.windows.net/";
                    }
                    chkSSL.Enabled = false;
                    doc.Load("uploadblobtoazure.xml");
                    XmlNodeList xmlnl = doc.SelectNodes("xml/configuration");
                    foreach (XmlNode xml in xmlnl)
                    {
                        XmlNode node = xml.SelectSingleNode("storageaccount");
                        if (node.InnerText.ToString() == cmbStorageSelect.SelectedItem.ToString())
                        {
                            accountKey = xml.SelectSingleNode("managementkeys").InnerText;
                            break;
                        }
                    }

                    try
                    {

                        blobClient = new CloudBlobClient(blobEndPoint, new StorageCredentialsAccountAndKey(storageAccName, accountKey));
                        treeContainerList.Nodes.Clear();
                        treeContainerList.Nodes.Add(storageAccName);

                        TreeNode tn = treeContainerList.Nodes[0];
                        //containerList = blobClient.ListContainers();
                        pictureBoxTreeView.Show();
                        foreach (var container in blobClient.ListContainers())
                        {
                            tn.Nodes.Add(container.Name);
                        }

                        pictureBoxTreeView.Hide();
                        //manageBlobToolStripMenuItem.Enabled = true;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("There is some error in fetching the containers for the storage account and the key combination,Either Correct the Combination or try with some other account that may be in the list.");
                        pictureBoxTreeView.Hide();
                    }
                }
            }
        }
    }
}
